package net.wanho.hospital.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.wanho.hospital.po.Drug;
import net.wanho.hospital.service.DrugService;
import net.wanho.hospital.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;
import java.io.Serializable;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 汤小洋
 * @since 2023-12-27
 */
@Api(tags = "接口")
@RestController
@RequestMapping("/drug")
@RequiredArgsConstructor
public class DrugController {

    private final DrugService drugService;

    @ApiOperation("分页查询")
    @GetMapping("/{pageNum}/{pageSize}")
    public AjaxResult findPage(@ApiParam("当前页") @PathVariable int pageNum,@ApiParam("页大小") @PathVariable int pageSize, Drug drug){
        IPage page = drugService.findPage(pageNum,pageSize,drug);
        return AjaxResult.success(page);
    }

    @ApiOperation("按主键查询")
    @GetMapping("/findDrug")
    public AjaxResult getById(@ApiParam("学号") Integer drId){
        return AjaxResult.success(drugService.getById(drId));
    }

    @ApiOperation("新增")
    @PostMapping
    public AjaxResult add(@RequestBody Drug stu){
        drugService.save(stu);
        return AjaxResult.success();
    }

    @ApiOperation("修改")
    @PutMapping
    public AjaxResult edit(@RequestBody Drug drug){
        drugService.updateById(drug);
        return AjaxResult.success();
    }

    @ApiOperation("按主键删除")
    @DeleteMapping("/{drId}")
    public AjaxResult removeById(@ApiParam("学号") @PathVariable Serializable drId){
        drugService.removeById(drId);
        return AjaxResult.success();
    }

    @GetMapping("/findAllDrugs")
    public AjaxResult findAllDrugs(
            @RequestParam("pageNumber") Integer pageNum,
            @RequestParam("size") Integer pageSize,
            @RequestParam("query") String drName
    ){
        Map<String,Object> map =  drugService.findAllDrugs(pageNum,pageSize,drName);
        return AjaxResult.success(map);
    }

    @GetMapping("/reduceDrugNumber")
    public AjaxResult reduceDrugNumber(
            @RequestParam("drId") Integer drId,
            @RequestParam("usedNumber") Integer usedNumber
    ){
        drugService.reduceDrugNumber(drId,usedNumber);
        return AjaxResult.success();
    }

}
